package org.apmap.servlets;

import org.hibernate.Session;
import org.apmap.base.Flat;
import org.apmap.base.Main;
import org.apmap.base.User;
import org.apmap.base.Office;
import org.apmap.beans.Points;
import org.apmap.common.Utils;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;
import java.io.IOException;
import java.io.Serializable;

public class StoreHousesServlet extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Session hibsession = null;
        try {
            JAXBContext jaxbContext = JAXBContext.newInstance(Points.class);
            Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
            Points points = (Points) unmarshaller.unmarshal(request.getInputStream());

            hibsession = Main.getSession();
            hibsession.beginTransaction();

            for (org.apmap.beans.Flat f : points.getFlatList()) {
                Flat flat = new Flat();
                BeanUtils.copyProperties(flat, f);


                if(flat.getId() == 0) {
                    Utils.prepareBeforeSave(flat);
                    User user = (User) request.getSession().getAttribute("user");
                    flat.setUser(user);
                    Serializable id = hibsession.save(flat);
//                response.getOutputStream().print(id.toString());
                } else {
                    Flat oldflat = (Flat) hibsession.load(Flat.class, flat.getId());
                    Utils.copyGeo(flat, oldflat);
                    hibsession.update(oldflat);
                }
            }

            for (org.apmap.beans.Office f : points.getOfficeList()) {
                Office office = new Office();
                BeanUtils.copyProperties(office, f);


                if(office.getId() == 0) {
                    Utils.prepareBeforeSave(office);
                    User user = (User) request.getSession().getAttribute("user");
                    office.setUser(user);
                    Serializable id = hibsession.save(office);
//                response.getOutputStream().print(id.toString());
                } else {
                    Office oldOffice = (Office) hibsession.load(Office.class, office.getId());
                    Utils.copyGeo(office, oldOffice);
                    hibsession.update(oldOffice);
                }
            }


            hibsession.getTransaction().commit();

        } catch (Exception ex) {
            if (hibsession != null) {
                hibsession.getTransaction().rollback();
            }
            throw new ServletException(ex);
        } finally {
            if (hibsession != null) {
                hibsession.close();
            }
        }
    }

}